package com.yvan.codeslave.snippet;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ExceptionUtil;
import com.yvan.codeslave.PluginUtils;
import org.apache.log4j.Level;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * @author luoyifan
 * @date 2018/9/25 20:29:00
 */
public class Idea {

    private static final Logger log = Logger.getInstance("CodeSlave");

    static {
        log.setLevel(Level.INFO);
    }

    public static String input(String tital) {
        return Messages.showInputDialog(
                PluginUtils.getProject(),
                tital,
                "询问",
                Messages.getQuestionIcon()
        );
    }

    public static void copy(String text) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(text);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }

    public static void showInfo(String content) {
        Messages.showMessageDialog(
                PluginUtils.getProject(),
                content,
                "提示",
                Messages.getInformationIcon()
        );
    }

    public static void showError(String content) {
        Messages.showMessageDialog(
                PluginUtils.getProject(),
                content,
                "错误提示",
                Messages.getErrorIcon()
        );
    }

    public static void notifyError(String content) {
        Notifications.Bus.notify(new Notification(
                "codeslave",
                "错误",
                content,
                NotificationType.ERROR));
    }

    public static void notifyInfo(String content) {
        Notifications.Bus.notify(new Notification(
                "codeslave",
                "提示",
                content,
                NotificationType.INFORMATION));
    }

    public static void rethrow(Exception ex) {
        ExceptionUtil.rethrow(ex);
    }
}
